<template>

  <div class="add-edit-dict-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.dict_type')">
        <el-input v-model="editDictTypeInfo.name" :placeholder="$t('tables_title.dict_type')" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.dict_name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="$t('tables_title.dict_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.dict_code')" prop="code" >
        <el-input v-model="ruleForm.code" :placeholder="$t('tables_title.dict_code')" v-if="!isEdit"></el-input>
        <el-input v-model="ruleForm.code" disabled v-else></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.super_dict')" prop="parentName">
        <div class="deptname-input" @click="innerVisible = true"></div>
        <el-input v-model="ruleForm.parentName" :placeholder="$t('tables_title.super_dict')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.sort')" prop="sort">
        <el-input v-model="ruleForm.sort" :placeholder="$t('tables_title.sort')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.dict_desc')">
        <el-input v-model="ruleForm.description" :placeholder="$t('tables_title.dict_desc')" type="textarea"></el-input>
      </el-form-item>

      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>

    <el-dialog :title="$t('tables_title.super_dict')" width="350px" :visible.sync="innerVisible" append-to-body>
        <div class="set-pname-view">
          <div class="pname-data-view">
            <el-tree  :data="treeData" :props="treeProps" node-key="id" default-expand-all :expand-on-click-node="false"
              @node-click="handleNodeClick"></el-tree>
          </div>
          <div style="text-align: center;">
            <el-button @click="innerVisible = false">{{$t('global.cancel')}}</el-button>
          </div>
        </div>
    </el-dialog>
  </div>
</template>

<script>

import { getDictDetails,editDict,addDict,getDictTree } from '@/api/dictApi.js'
import { getDictTreeData } from '@/common/utils/util.js'

export default {
  data(){
    return{
      loading:false,
      innerVisible:false,
      treeData:[],
      treeProps: {
        children: 'children',
        label: 'name'
      },
      ruleForm:{
        name:'',
        code:'',
        parentId:'',
        parentName:'',
        sort:'',
        description:''
      },
      rules:{
          name: [
            { required: true, message: this.$t('dict_management.empty_dict_name'), trigger: 'blur' },
          ],
          code: [
            { required: true, message: this.$t('dict_management.empty_dict_code'), trigger: 'blur' },
          ],
          parentName: [
            { required: true, message: this.$t('dict_management.empty_super_dict'), trigger: 'blur' },
          ],
          sort: [
            { required: true, message: this.$t('global.empty_sort'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ['isEdit','editDictTypeInfo','editDictInfo'],
  watch:{
    editDictInfo(newVal,oldVal){
      this.ruleForm = newVal
      this.getDetails()
    },
    editDictTypeInfo(newVal,oldVal){
      this.getDictTreeByTypeId()
    }
  },
  components:{

  },
  created(){
    this.ruleForm = this.editDictInfo
    this.getDetails()
    this.getDictTreeByTypeId()
  },
  methods:{
    handleNodeClick(data){
      this.ruleForm.parentId = data.id
      this.ruleForm.parentName = data.name
      this.innerVisible = false
    },
    getDetails(){
      if(this.ruleForm.dictId != undefined && this.ruleForm.dictId !=''){
        getDictDetails({dictId:this.ruleForm.dictId}).then(res=>{
          this.ruleForm = res.data.data
        })
      }
    },
    getDictTreeByTypeId(){
      getDictTree({dictTypeId:this.editDictTypeInfo.dictTypeId}).then(res=>{
        let data = getDictTreeData(res.data)
        this.treeData = res.data.filter(item=>{
          return item.id == 0
        })
        if(this.treeData.length > 0){
          this.treeData[0].children = data
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = new FormData()
          formData.append("dictTypeId",this.editDictTypeInfo.dictTypeId)
          formData.append("name",this.ruleForm.name)
          formData.append("code",this.ruleForm.code)
          formData.append("parentName",this.ruleForm.parentName)
          formData.append("parentId",this.ruleForm.parentId)
          formData.append("sort",this.ruleForm.sort)
          formData.append("description",this.ruleForm.description)
          if(this.ruleForm.dictId){
            formData.append("dictId",this.ruleForm.dictId)
            this.userEditDict(formData)
          }else{
            this.userAddDict(formData)
          }
        } else {
          return false;
        }
      });
    },
    userEditDict(formData){         //修改role信息
      this.loading = true
      editDict(formData).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    userAddDict(formData){          //新增管Role
      this.loading = true
      addDict(formData).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{
         this.loading = false
      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-dict-view{
  width: 100%;
  box-sizing: border-box;
  padding: 0 30px;
}

.set-pname-view{
  width: 100%;
  height: 500px;
  display: flex;
  flex-direction: column;
  .pname-data-view{
    flex: 1;
    overflow: auto;
    margin-bottom: 20px;
    margin-top: -10px;
  }
}
</style>
